<!DOCTYPE html>
<html class="x-admin-sm x-admin">
    
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css">
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
    </head>
    
    
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">演示</a>
                <a>
                    <cite>导航元素</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                           	<div class="layui-card-body ">
					<div class="layui-inline layui-show-xs-block">
								<input type="text" name="name" id="searchNameId"
								placeholder="请输入产品信息"
									autocomplete="off" class="layui-input">
							</div>
							<div class="layui-inline layui-show-xs-block">
								<button class="layui-btn" onclick="doGetObjects()" lay-submit="" lay-filter="sreach">
									<i class="layui-icon">&#xe615;</i>
								</button>
							</div>
						
					</div>
                           <!--  </form> -->
                        </div>
                        <div class="layui-card-header">
                            <button class="layui-btn layui-btn-danger" onclick="doDeleleObjects()">
                                <i class="layui-icon"></i>删除</button>
                            <button class="layui-btn" id="product-add">
                                <i class="layui-icon"></i>添加</button></div>
                        <div class="layui-card-body ">
                            <table class="layui-table">
                                <thead>
                                  <tr>
                               <th>
                               <input type="checkbox" id="checkAll" value="1"   lay-skin="primary">全选</th>
                                        <th>产品ID</th>
                                        <th>产品信息</th>
                                        <th>商品类型</th>
                                        <th>产品活动</th>
                                        <th>产品价格</th>
                                        <th>产品库存</th>
                                        <th>更新日期</th>
                                        <th>产品状态</th>
                                        <th>操作</th></tr>
                                        
                                </thead>
                                <tbody  class="text-d" id="tbodyId">
                                    <tr>
                                  <td colspan="7">数据正在加载中......
                                  </td>
								</tr>
                                </tbody>
                            </table>
                        <div class="layui-card-body ">
                         <div id="pageId" class="page" th:include="page.html :: pageFragement">
                       </div>
                    </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
  <script type="text/javascript">

 
//页面数据呈现模块及操作模块

    $(function(){
    	
     //初始化当前页码值
    // $("pageId").data("pagecurrent",1);
     //执行查询
    doGetObjects();
    doLoadUI("product-add","x/product-add");
    //debugger
  //注册事件
     $(".layui-show-xs-block")
	  .on("click",".layui-btn",doQueryObjects)
	  $("#checkAll").change(doChangeTBodyCheckBoxState);
  	$(".text-d")
  	  .on("change","input:checkbox",doChangeTHeadCheckBoxState);
    })
 
    function doQueryObjects(){
	$("#pageId").data("pageCurrent",1);
	
	doGetObjects();
}





 
    //处理tbody中checkbox对象的事件
   function doChangeTHeadCheckBoxState(){
	  //1.定义一个初始状态，默认值true.
	  var flag=true;
	  //2.获得所有tbody中的checkbox对象并与flag变量的初始状态进行逻辑与操作
	  $(".text-d input:checkbox")
	  .each(function(){
		  flag=flag&&$(this).prop("checked");
	  });
	  //3.修改thead中checkbox状态
	  $("#checkAll").prop("checked",flag);
   }
 function doChangeTBodyCheckBoxState(){
	   //1.获得head中checkbox对象的状态
	   var flag=$(this).prop("checked");
	   //2.修改tbody中checkbox对象状态
	   $(".text-d input:checkbox").prop("checked",flag);
   }
function doDeleleObjects(){
	debugger
	 var idArray=doGetCheckedIds();//通过此函数返回一个数组，数组中存储的是选中的记录id
	   if(idArray.length==0){
		   alert("请先选择");
		   return;
	   }
	   if(!confirm("确认删除吗"))return;
	   //2.并构建参数对象
	   var params={"ids":idArray.toString()};//[1,2,3]-->1,2,3
	   //3.定义删除操作的url
	   const url="item/doDeleteObjects";
	  
	   //4.发送异步请求执行删除操作，并刷新页面
	   $.post(url,params,doHandleDeleteResult)
	   $("#checkAll").prop("checked",false);
	   
}
function doHandleDeleteResult(result){
	   if(result.state==1){
		   alert(result.message);
		   //doGetObjects();//刷新
		   doRefreshAfterDeleteOK();
	   }else{
		   alert(result.message);
	   }
}
function doRefreshAfterDeleteOK(){
	   var checkAll=$("#checkAll").prop("checked");
	   var pageCurrent=$("#pageId").data("pageCurrent");
	   var pageCount=$("#pageId").data("pageCount")
	   if(checkAll&&pageCurrent==pageCount&&pageCount>1){
		   pageCurrent--;
		   $("#pageId").data("pageCurrent",pageCurrent);
	   }
	   doGetObjects();
}
function doGetCheckedIds(){
	   //1.定义数组
	   var idArray=[];
	   //2.迭代所有选中的checkbox对象，并获取value属性的值，然后存储到数组
	   $(".text-d input:checkbox:checked")
	   .each(function(){
		   idArray.push($(this).val());
	   });
	   //3.返回数组
	   return idArray;
}
    //通过此函数异步加载服务端的日志信息
    function doGetObjects(){
    	//debugger
     //初始化全选checkbox对象状态
    // $("#checkAll").prop("checked",false);
     //定义请求参数
     var pageCurrent=$("#pageId").data("pageCurrent");
     if(!pageCurrent)pageCurrent=1;
     
     //参数封装:
      let params={"pageCurrent":pageCurrent};
      
      //debugger
      var name=$("#searchNameId").val();
      
      if(name)params.name=name;
     //定义请求url
     const url="item/doFindPageObjects";
     //发送异步请求并处理响应结果
     console.log(params);
     $.getJSON(url,params,function(result){
      doHandleQueryResult(result);
     });
    }
    function doHandleQueryResult(result){
     console.log("result",result);
     if(result.state==1){
    	
      //第一步:将当前页的日志记录信息更新到页面上
      console.log(result.data.records);
      doSetTableBodyRows(result.data.records);
     //第二步:将分页信息更新到页面上
     doSetPagination(result.data);
     }else{
      doSetQueryErrors(result.message);
     }
    }
   function doSetQueryErrors(message){
     $("#tbodyId").html(`<tr><td colspan='7'>${message}</td><tr>`);
    }
    function doSetTableBodyRows(records){
     //获取tbody对象并清空内容
     var tBody=$("#tbodyId");
     tBody.empty();
    
     //遍历records,并将结果更新到页面上
     records.forEach(record=>tBody.append(doCreateRow(record)))
    }
   function doCreateRow(record){
	
	     return `<tr>
	     <td><input type="checkbox" name=""  lay-skin="primary" value='${record.id}'></td>
	     	<td>${record.id}</td>
	        <td>${record.name}</td>
	        <td>${record.property}</td>
	        <td>${record.activity}</td>
	        <td>${record.price}</td>
	        <td>${record.num}</td>
	        <td>${record.updated}</td>
	        <td class="td-status">
            <span class="layui-btn layui-btn-normal layui-btn-mini">正常</span></td>
	        <td class="td-manage">
	        <a onclick="member_stop(this,'10001')" href="javascript:;"  title="下架">
            	<i class="layui-icon">&#xe601;</i>
          	</a>
	          <a title="编辑"  onclick="xadmin.open('编辑','role-add.html')" href="javascript:;">
	            <i class="layui-icon">&#xe642;</i>
	          </a>
        </td>
	     </tr>`;
   }
   
       //商品状态
       function member_stop(obj,id){
          layer.confirm('确认要下架吗？',function(index){

              if($(obj).attr('title')=='下架'){

                //发异步把用户状态进行更改
                $(obj).attr('title','上架')
                $(obj).find('i').html('&#xe62f;');

                $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已下架');
                layer.msg('已下架!',{icon: 5,time:1000});

              }else{
                $(obj).attr('title','下架')
                $(obj).find('i').html('&#xe601;');

                $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('正常');
                layer.msg('已上架!',{icon: 5,time:1000});
              }
              
          });
      }
     

	</script>
</html>